﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace Calculator
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void PerformAddition(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;
            CreateLiteral(firstArgument, (sender as System.Web.UI.WebControls.Button).Text);
        }

        protected void PerformSubstraction(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;
            CreateLiteral(firstArgument, (sender as System.Web.UI.WebControls.Button).Text);
        }

        protected void PerformMultiplication(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;
            CreateLiteral(firstArgument, (sender as System.Web.UI.WebControls.Button).Text);
        }

        protected void PerformDivision(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;
            CreateLiteral(firstArgument, (sender as System.Web.UI.WebControls.Button).Text);
        }

        protected void PerformSqrt(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;

            try
            {
                double arg = double.Parse(firstArgument);
                this.FirstArgument.Text = Math.Sqrt(arg).ToString();
            }
            catch (Exception)
            {
                this.FirstArgument.Text = "Invalid";
            }
        }

        protected void PerformMod(object sender, EventArgs e)
        {
            string firstArgument = this.FirstArgument.Text;

            try
            {
                decimal arg = decimal.Parse(firstArgument);
                this.FirstArgument.Text = Math.Abs(arg).ToString();
            }
            catch (Exception)
            {
                this.FirstArgument.Text = "Invalid";
            }
        }

        protected void PerformCalculation(object sender, EventArgs e)
        {
            string firstArgument = this.ArgumentsWithOperation.Items[0].Value;
            string op = this.ArgumentsWithOperation.Items[1].Value;
            string secondArgument = this.SecondArgument.Text;

            EmptyLiterals();

            decimal firstArg = 0;
            decimal secondArg = 0;

            try
            {
                firstArg = decimal.Parse(firstArgument);
                secondArg = decimal.Parse(secondArgument);
                if (secondArg == 0 && op == "/")
                {
                    throw new Exception();
                }

                PerformCalculation(op, firstArg, secondArg);
            }
            catch (Exception)
            {
                this.FirstArgument.Text = "Invalid";
            }

            this.FirstArgument.Visible = true;
            this.SecondArgument.Visible = false;
            this.SecondArgument.Text = "";
        }
  
        private void PerformCalculation(string op, decimal firstArg, decimal secondArg)
        {
            switch (op)
            {
                case "+":
                    {
                        this.FirstArgument.Text = (firstArg + secondArg).ToString();
                    }
                    break;
                case "-":
                    {
                        this.FirstArgument.Text = (firstArg - secondArg).ToString();
                    }
                    break;
                case "/":
                    {
                        this.FirstArgument.Text = (firstArg / secondArg).ToString();
                    }
                    break;
                case "*":
                    {
                        this.FirstArgument.Text = (firstArg * secondArg).ToString();
                    }
                    break;
            }
        }
  
        private void EmptyLiterals()
        {
            while (this.ArgumentsWithOperation.Items.Count != 0)
            {
                this.ArgumentsWithOperation.Items.RemoveAt(0);
            }
        }

        private bool ValidateArg(string arg)
        {
            decimal a = 0;
            return decimal.TryParse(arg, out a);
        }

        private void CreateLiteral(string arg, string operation)
        {
            if (!ValidateArg(arg))
            {
                this.FirstArgument.Text = "Invalid";
                return;
            }

            this.ArgumentsWithOperation.Items.Add(new ListItem() { Value = arg });
            this.ArgumentsWithOperation.Items.Add(new ListItem() { Value = operation });
            this.FirstArgument.Visible = false;
            this.SecondArgument.Visible = true;
        }
    }
}